package com.yunke.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunke.common.exception.BusinessException;
import com.yunke.entity.YunkeHomeModule;
import com.yunke.mapper.YunkeHomeModuleMapper;
import com.yunke.service.YunkeHomeModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 首页模块配置服务实现类
 * 
 * @author yunke
 * @since 1.0.0
 */
@Service
public class YunkeHomeModuleServiceImpl extends ServiceImpl<YunkeHomeModuleMapper, YunkeHomeModule> implements YunkeHomeModuleService {

    @Autowired
    private YunkeHomeModuleMapper homeModuleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createModule(YunkeHomeModule module) {
        if (StrUtil.isBlank(module.getModuleName())) {
            throw new BusinessException("模块名称不能为空");
        }

        module.setCreateTime(System.currentTimeMillis());
        module.setUpdateTime(System.currentTimeMillis());
        module.setStatus(0);

        // 设置默认值
        if (module.getIsEnabled() == null) {
            module.setIsEnabled(true);
        }
        if (module.getIsTop() == null) {
            module.setIsTop(false);
        }
        if (module.getTargetUser() == null) {
            module.setTargetUser(1); // 全部用户
        }
        if (module.getDisplayOrder() == null) {
            module.setDisplayOrder(0);
        }
        if (module.getClickCount() == null) {
            module.setClickCount(0);
        }
        if (module.getViewCount() == null) {
            module.setViewCount(0);
        }

        return save(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateModule(YunkeHomeModule module) {
        YunkeHomeModule existModule = getById(module.getId());
        if (existModule == null) {
            throw new BusinessException("模块不存在");
        }

        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteModule(Long id) {
        YunkeHomeModule module = getById(id);
        if (module == null) {
            throw new BusinessException("模块不存在");
        }

        module.setStatus(1);
        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteModules(List<Long> ids) {
        return homeModuleMapper.updateModuleStatus(ids, 1) > 0;
    }

    @Override
    public IPage<YunkeHomeModule> getModulePage(Integer current, Integer size,
                                               Integer moduleType, String displayPosition,
                                               String moduleName, Boolean isEnabled) {
        Page<YunkeHomeModule> page = new Page<>(current, size);
        return homeModuleMapper.selectModulePage(page, moduleType, displayPosition, moduleName, isEnabled);
    }

    @Override
    public YunkeHomeModule getModuleById(Long id) {
        return getById(id);
    }

    @Override
    public List<YunkeHomeModule> getModulesByPosition(String displayPosition, Integer targetUser, Boolean isEnabled) {
        return homeModuleMapper.selectModulesByPosition(displayPosition, targetUser, isEnabled);
    }

    @Override
    public List<YunkeHomeModule> getModulesByType(Integer moduleType, Boolean isEnabled) {
        return homeModuleMapper.selectModulesByType(moduleType, isEnabled);
    }

    @Override
    public List<YunkeHomeModule> getHomeModules(Integer targetUser) {
        return homeModuleMapper.selectHomeModules(targetUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean enableModule(Long id) {
        YunkeHomeModule module = getById(id);
        if (module == null) {
            throw new BusinessException("模块不存在");
        }

        module.setIsEnabled(true);
        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disableModule(Long id) {
        YunkeHomeModule module = getById(id);
        if (module == null) {
            throw new BusinessException("模块不存在");
        }

        module.setIsEnabled(false);
        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean topModule(Long id) {
        YunkeHomeModule module = getById(id);
        if (module == null) {
            throw new BusinessException("模块不存在");
        }

        module.setIsTop(true);
        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean untopModule(Long id) {
        YunkeHomeModule module = getById(id);
        if (module == null) {
            throw new BusinessException("模块不存在");
        }

        module.setIsTop(false);
        module.setUpdateTime(System.currentTimeMillis());
        return updateById(module);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateModuleStatus(List<Long> ids, Integer status) {
        return homeModuleMapper.updateModuleStatus(ids, status) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateModuleEnabled(List<Long> ids, Boolean isEnabled) {
        return homeModuleMapper.updateModuleEnabled(ids, isEnabled) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateClickCount(Long id) {
        return homeModuleMapper.updateClickCount(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateViewCount(Long id) {
        return homeModuleMapper.updateViewCount(id) > 0;
    }

    @Override
    public long countModules(Integer moduleType, String displayPosition, Boolean isEnabled) {
        return homeModuleMapper.countModules(moduleType, displayPosition, isEnabled);
    }
} 